﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HumanManagement.BusinessLogic;
using HumanManagement.BusinessObjects;
using System.Data;

namespace BSC_HumanManagement.Pages
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["login"].ToString() == "true")
            {
                Response.Redirect("/Default.aspx");
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string adPath = "LDAP://bsc.com.vn/CN=Users,DC=bsc,DC=com,DC=vn";       
            BSC_HumanManagement.Code.LdapAuthentication adAuth = new BSC_HumanManagement.Code.LdapAuthentication(adPath);
            try
            {
                if (true != adAuth.IsAuthenticated("bschn", txtUsername.Text, txtPassword.Text))
                {
                    if (CheckUserFunction() == false)
                    {
                        this.errortext.Text = "Tài khoản này chưa được gán quyền";
                    }
                    else
                    {
                        Session["login"] = "true";
                        string aa = Session["login"].ToString();
                        Session["login_username"] = txtUsername.Text;
                        Session["domainPath"] = adPath;
                        Response.Redirect("/Default.aspx", false);
                    }
                }
                else
                {
                    errortext.Text = "Error username or password. ";
                }
            }
            catch (Exception ex)
            {
                errortext.Text = "Error authenticating. " + ex.Message;
            }
        }

        private bool CheckUserFunction()
        {
            UserBL objUserBL = new UserBL();
            DataTable _objDataTable = objUserBL.GetDataSet().Tables[0];
            foreach (DataRow item in _objDataTable.Rows)
            {
                if (item["UserName"].ToString().Equals(txtUsername.Text))
                {
                    // Nếu là User chưa đc gán quyền với bảng Personal Profile thì báo cho ng dùng
                    if (item["UserType"].ToString().Equals("Staff") && item["PersonalProfileID"].ToString() == "")
                    {
                        return false;
                    }
                    // set session UserOrStaff là User hay Staff 
                    Session["PersonalProfileID"] = item["PersonalProfileID"].ToString();
                    PersonalProfileBL _objPersonalProfileBL = new PersonalProfileBL();
                    PersonalProfile _objPersonalProfile = _objPersonalProfileBL.GetByID(Convert.ToInt32(item["PersonalProfileID"].ToString()), Convert.ToInt32(item["PersonalProfileID"].ToString()));
                    Session["PersonalInfomation"] = _objPersonalProfile;
                    Session["BranchID"] = _objPersonalProfile.BranchID;
                    if (item["UserType"].ToString().Equals("Staff"))
                    {
                        Session["UserOrStaff"] = "Staff";
                    }
                    else
                    {
                        Session["UserOrStaff"] = "User";
                    }

                    
                }
            }
            return true;
        }
    }
}